package com.bf.hotpoint.repository.mongo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.GeospatialIndex;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.IndexDefinition;
import org.springframework.stereotype.Repository;

@Repository
public class CommonMongoRepository {

	@Autowired
	private MongoTemplate mongoTemplate;
	
	public void initCollection(Class className)
	{
		if(!mongoTemplate.collectionExists(className))
		{
			mongoTemplate.createCollection(className);
		}
		
	}
	
	public List findAllByClass(Class className)
	{
		return mongoTemplate.findAll(className);
	}
	
	public void indexGeo(String field,Class classname){
		GeospatialIndex geospatialIndex = new GeospatialIndex(field);
		mongoTemplate.indexOps(classname).ensureIndex(geospatialIndex);
	}
	
	public void index(String field,Class classname,Direction direction){
		IndexDefinition index = new Index(field,direction);
		mongoTemplate.indexOps(classname).ensureIndex(index);
	}
}
